package com.hghivln.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hghivln.blog.common.lang.Result;
import com.hghivln.blog.entity.Blog;
import com.hghivln.blog.entity.BlogCollection;
import com.hghivln.blog.entity.Type;
import com.hghivln.blog.mapper.BlogCollectionMapper;
import com.hghivln.blog.mapper.BlogMapper;
import com.hghivln.blog.mapper.TypeMapper;
import com.hghivln.blog.pojo.dto.BlogCollectionDto;
import com.hghivln.blog.pojo.vo.BlogVo;
import com.hghivln.blog.service.BlogCollectionService;
import com.hghivln.blog.utils.PagesUtils;
import com.hghivln.blog.utils.ShiroUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 用户_博客收藏表 服务实现类
 * </p>
 *
 * @author hghivln
 * @since 2021-09-25
 */
@Service
public class BlogCollectionServiceImpl extends ServiceImpl<BlogCollectionMapper, BlogCollection> implements BlogCollectionService {

    @Autowired
    private BlogMapper blogMapper;

    @Autowired
    private TypeMapper typeMapper;

    @Override
    public void collection(Long blogId) {
        Long id = ShiroUtils.getLoginUser().getId();
        BlogCollection blogCollection = new BlogCollection();
        blogCollection.setBlogId(blogId);
        blogCollection.setUserId(id);
        getBaseMapper().insert(blogCollection);
        Blog blog = blogMapper.selectById(blogId);
        blog.setBlogCollection(blog.getBlogCollection()+1);
        blogMapper.updateById(blog);
    }

    @Override
    public Integer getCollection(Long blogId) {
        Long id = ShiroUtils.getLoginUser().getId();
        return getBaseMapper().getByColIdAndUserId(blogId,id);
    }

    @Override
    public void removeByBlogId(Long blogId) {
        QueryWrapper<BlogCollection> qw = new QueryWrapper<>();
        Long id = ShiroUtils.getLoginUser().getId();
        qw.eq("blog_id",blogId);
        qw.eq("user_id",id);
        getBaseMapper().delete(qw);
        Blog blog = blogMapper.selectById(blogId);
        blog.setBlogCollection(blog.getBlogCollection()-1);
        blogMapper.updateById(blog);

    }

    @Override
    public Result<?> getByPage(BlogCollectionDto blogCollectionDto) {
        Long id = ShiroUtils.getLoginUser().getId();
        QueryWrapper<BlogCollection> qw = new QueryWrapper<>();
        qw.eq("user_id",id);
        qw.orderByDesc("collection_time");
        List<BlogCollection> blogCollectionList = getBaseMapper().selectList(qw);
        if (!CollectionUtils.isEmpty(blogCollectionList)){
            List<Long> blogIds = new ArrayList<>();
            blogCollectionList.forEach(e -> {
                blogIds.add(e.getBlogId());
            });
            // 查询博客
            List<Blog> blogList = blogMapper.getBlogByIds(blogIds);
            // 封装分类
            List<BlogVo> blogVoList = blogList.stream().map(blog -> {
                BlogVo blogVo = new BlogVo();
                BeanUtils.copyProperties(blog, blogVo);
                return blogVo;
            }).peek(blogVo -> {
                // 根据分类id取出分类列表中对应的数据
                Long typeId = blogVo.getBlogType();
                Type type = typeMapper.selectById(typeId);
                blogVo.setTypeName(type.getTypeName());
            }).collect(Collectors.toList());
            Page pages = PagesUtils.getPages(blogCollectionDto.getPage(), blogCollectionDto.getLimit(), blogVoList);
            return new Result<>(pages.getTotal(),pages.getRecords());
        } else {
            return new Result<>(0L, new ArrayList<>(0));
        }

    }
}
